package com.yisu.auction.api.core.service.impl;

import com.google.common.collect.Lists;
import com.yisu.auction.api.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.api.core.mapper.PrizeMapper;
import com.yisu.auction.api.core.mapper.ShowMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.IShowService;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.prizePackage.MineOrderRequest;
import com.yisu.auction.api.domain.request.prizePackage.PrizeDetailRequest;
import com.yisu.auction.api.domain.vo.PrizePackageVos.ShowInfoVo;
import com.yisu.auction.api.domain.vo.PrizePackageVos.ShowOrderListVo;
import com.yisu.auction.ewf.entity.Prize;
import com.yisu.auction.ewf.entity.Show;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/21
 */
@Service("IShowService")
public class ShowServiceImpl extends BaseService implements IShowService {

    @Resource
    private ShowMapper showMapper;
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;

    /**
     * author:黄萧林
     * Business Description: 添加我的晒单
     *
     * @param mineOrderRequest
     * @return
     */
    @Override
    public boolean addShowOrder(MineOrderRequest mineOrderRequest) {
        Prize prizeVo = getPrizeById(mineOrderRequest.getPrizeId());
        Show show = new Show();
        show.setCreateDate(new Date());
        show.setIsValid(true);
        show.setPrizeId(mineOrderRequest.getPrizeId());
        show.setImages(mineOrderRequest.getImages());
        show.setMemberId(Long.valueOf(mineOrderRequest.getMemberId()));
        show.setTitle(mineOrderRequest.getTitle());
        show.setDescrib(mineOrderRequest.getDescrib());
        show.setStatus(0);
        show.setName(prizeVo.getName());
        show.setProductId(prizeVo.getGoodsId());
        Prize checkModel = prizeMapper.selectByPrimaryKey(mineOrderRequest.getPrizeId());
        Prize updateModel = new Prize();
        updateModel.setId(mineOrderRequest.getPrizeId());
        updateModel.setShowOrderTime(new Date());
        if (checkModel == null) {
            prizeArchiveMapper.updateByPrimaryKeySelective(updateModel);
        } else {
            prizeMapper.updateByPrimaryKeySelective(updateModel);
        }
        return showMapper.insertSelective(show) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:获取订单详细信息
     *
     * @param id
     * @return
     */
    @Override
    public ShowInfoVo getShowInfo(Long id) {
        return showMapper.getOrderInformation(id);
    }

    /**
     * author:黄萧林
     * Business Description:获取我的订单列表信息
     *
     * @return
     */
    @Override
    public List<ShowOrderListVo> getMineShowOrderList(BasePage basePage) {
        return showMapper.getMineShowOrderList(basePage);
    }

    /**
     * author:黄萧林
     * Business Description:获取订单列表信息
     *
     * @param prizeDetailRequest
     * @return
     */
    @Override
    public List<ShowOrderListVo> getShowOrderList(PrizeDetailRequest prizeDetailRequest) {
        //TODO 写死的状态
        prizeDetailRequest.setPrizeId(0);
        List<ShowOrderListVo> showOrderListVos = showMapper.getShowOrderList(prizeDetailRequest);
        for (ShowOrderListVo item : showOrderListVos) {
            if (!StringUtils.isEmpty(item.getImages())) {
                String[] imgArray = item.getImages().split(",");
                if (imgArray.length > 3) {
                    List<String> convertTemp = Lists.newArrayList(imgArray[0], imgArray[1], imgArray[2]);
                    item.setImages(StringUtils.join(convertTemp, ","));
                }
            }
        }
        return showOrderListVos;
    }
}
